package com.hfzy.ihk.web.userCenter.biz;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hfzy.ihk.common.util.Utils;
import com.hfzy.ihk.facade.userCenter.entity.UserHasCallLine;
import com.hfzy.ihk.facade.userCenter.entity.UserHasRole;
import com.hfzy.ihk.facade.userCenter.entity.UserInfo;
import com.hfzy.ihk.facade.userCenter.enums.CallLineType;
import com.hfzy.ihk.facade.userCenter.enums.CallType;
import com.hfzy.ihk.web.oauth.client.utils.UserUtil;
import com.hfzy.ihk.web.userCenter.mapper.UserHasCallLineMapper;
import com.hfzy.ihk.web.userCenter.mapper.UserHasRoleMapper;
import com.hfzy.ihk.web.userCenter.mapper.UserMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Component
@Slf4j
public class UserHasCallLineBiz {
    @Autowired
    UserHasCallLineMapper userHasCallLineMapper;

    @Autowired
    UserHasRoleMapper userHasRoleMapper;

    @Autowired
    UserMapper userMapper;

    @Transactional
    public boolean saveUserHasCallLineByRole(int roleId, CallType callType, CallLineType callLineType){
        int successCount = 0;
        List<Integer> userIds = new ArrayList<>();
        /*QueryWrapper<UserHasRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id",roleId);
        List<UserHasRole> userHasRoles = userHasRoleMapper.selectList(queryWrapper);
        if(userHasRoles!=null && !userHasRoles.isEmpty()){
            userHasRoles.forEach(it->{userIds.add(it.getUserId());});
        }*/

        userIds = userMapper.selectUserIdsByRoleId(roleId);
        if(userIds!=null && !userIds.isEmpty()){
            for(Integer uId:userIds){
                UserHasCallLine userHasCallLine = new UserHasCallLine();
                userHasCallLine.setCallType(callType);
                userHasCallLine.setCallLineType(callLineType);
                userHasCallLine.setEnabled(true);
                userHasCallLine.setUserId(uId);
                int count = saveUserHasCallLine(userHasCallLine);
                if(count>0)
                    successCount++;
            }
        }
        return successCount>0?true:false;
    }


    @Transactional
    public boolean saveUserHasCallLineByUserId(String id,int userId, CallType callType, CallLineType callLineType) {
        UserHasCallLine userHasCallLine = null;
        if(!Utils.isNullString(id)){
            userHasCallLine = userHasCallLineMapper.selectById(id);
            UserHasCallLine oldUserHasCallLine = userHasCallLineMapper.findUserHasCallLineByUserIdAndType(userId,callType);
            if(oldUserHasCallLine!=null && userHasCallLine.getId()!=oldUserHasCallLine.getId()){
                throw new IllegalArgumentException("不能对同一个用户绑定多个相同的外呼类型关系");
            }
        }else{
            userHasCallLine = new UserHasCallLine();
        }
        userHasCallLine.setCallType(callType);
        userHasCallLine.setCallLineType(callLineType);
        userHasCallLine.setEnabled(true);
        userHasCallLine.setUserId(userId);
        return saveUserHasCallLine(userHasCallLine)>0?true:false;
    }

    @Transactional
    public int saveUserHasCallLine(UserHasCallLine userHasCallLine){
        //判断该用户登录名是否存在
        if(userHasCallLine==null)
            throw new IllegalArgumentException("数据异常");
        if(userHasCallLine.getId()!=null){//更新
            return update(userHasCallLine);
        }else{//插入
            return insert(userHasCallLine);
        }
    }

    /**
     * 目前设定是1对1的形式
     * */
    public  int insert(UserHasCallLine userHasCallLine){
        //根据userid查找有没有 设置过 线路信息
        UserHasCallLine oldObj = userHasCallLineMapper.findUserHasCallLineByUserIdAndType(userHasCallLine.getUserId(),userHasCallLine.getCallType());
        UserInfo loginUser = UserUtil.getLoginUser();
        Date date = new Date();//更新时间
        //原本不存在则进行绑定，否则跳过
        if(oldObj==null){
            if(loginUser!=null){
                userHasCallLine.setCreateTime(date);
                userHasCallLine.setModTime(date);
                userHasCallLine.setModUserId(loginUser.getId());
                userHasCallLine.setCreateUserId(loginUser.getId());
            }
            userHasCallLine.setEnabled(true);
            return userHasCallLineMapper.insert(userHasCallLine);
        }else{
            if(!oldObj.isEnabled()){
                oldObj.setEnabled(true);
                if(loginUser!=null) {
                    oldObj.setModTime(date);
                    oldObj.setModUserId(loginUser.getId());
                    oldObj.setCallLineType(userHasCallLine.getCallLineType());
                }
                return userHasCallLineMapper.updateById(oldObj);
            }
        }
        return 0;
    }

    public int update(UserHasCallLine userHasCallLine){
        UserInfo loginUser = UserUtil.getLoginUser();
        if(loginUser!=null){
            userHasCallLine.setModTime(new Date());
            userHasCallLine.setModUserId(loginUser.getId());
        }
        return userHasCallLineMapper.updateById(userHasCallLine);
    }
}
